package com.hxwm.hwdbtool.controller.dialog;

import com.hxwm.hwdbtool.core.DatabaseConnectionListener;
import com.hxwm.hwdbtool.entity.dbconnect.HwDateSource;
import com.hxwm.hwdbtool.util.JavaFXUIControlUtil;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.stage.Stage;
import lombok.Data;
import org.apache.log4j.Logger;

import java.net.URL;
import java.util.ResourceBundle;

@Data
public class BaseDBConnectDialog implements Initializable {
    Logger logger = Logger.getLogger(BaseDBConnectDialog.class);
    @FXML
    private DialogPane connectDialog;
    @FXML
    private TextArea dbInfoTextArea;
    @FXML
    private TextField dbUserTextField;
    @FXML
    private TextField dbNameTextField;
    @FXML
    private TextField dbIpTextField;
    @FXML
    private TextField dbPortTextField;
    @FXML
    private PasswordField dbPasswordField;
    @FXML
    private Button recognitionBtn;
    @FXML
    private Button dbConnTestBtn;
    @FXML
    private Button confirmBtn;
    @FXML
    private ComboBox dbTypeComboBox;
    private String dbType;

    private DatabaseConnectionListener listener;

    public DatabaseConnectionListener getListener() {
        return listener;
    }

    public void setListener(DatabaseConnectionListener listener) {
        this.listener = listener;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        // 初始化操作，可以在这里添加事件监听器等
        dbTypeComboBox.getItems().addAll("MySql","DB2","Oracle","SQL Server","Informix");

        // 添加值改变监听器
        dbTypeComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
            switch ((String)newValue){
                case "MySql":
                    dbType = "mysql";
                    break;
                case "DB2":
                    dbType = "db2";
                    break;
                case "Oracle":
                    dbType = "oracle";
                    break;
                case "SQL Server":
                    dbType = "sqlserver";
                    break;
                case "Informix":
                    dbType = "informix-sqli";
                    break;
                default:
                    break;
            }
        });
    }

    @FXML
    private void handleRecognition(){
        String dbInfo = dbInfoTextArea.getText();
        String[] dbInfoArray = dbInfo.split("\\n");//使用换行分割
        if(dbInfoArray != null && dbInfoArray.length < 3){
            dbInfoArray = dbInfo.split("\\s+");//使用空格分割
        }
        if(dbInfoArray != null && dbInfoArray.length >= 3){
            String dbUrl = dbInfoArray[0];
            String[] dbUrlArray = dbUrl.split("[:/]+");
            int length = dbUrlArray.length;

            if(length >= 2){
                dbType = dbUrlArray[1];
                dbTypeComboBox.setValue(dbType);
            }
            if(length >= 3){
                dbIpTextField.setText(dbUrlArray[2]);
            }
            if(length >= 4){
                dbPortTextField.setText(dbUrlArray[3]);
            }
            if(length >= 5){
                dbNameTextField.setText(dbUrlArray[4]);
            }

            dbUserTextField.setText(dbInfoArray[1]);
            dbPasswordField.setText(dbInfoArray[2]);
        }
    }
    @FXML
    private void handleTestConnect(){
        HwDateSource dateSource = dialogDBInfo();
        JavaFXUIControlUtil.testDatabaseConnection(dateSource.getDbUrl(), dateSource.getDbUser(), dateSource.getDbPassword());
    }
    @FXML
    private void handleConfirm(){
        HwDateSource dateSource = dialogDBInfo();
        if (listener != null) {
            if(JavaFXUIControlUtil.testDatabaseConnection(dateSource)) {
                listener.onDatabaseConnected(dateSource);
            }else{
                JavaFXUIControlUtil.showInformationDialog("错误","添加数据源失败，连接失败");
            }
        }
        ((Stage)connectDialog.getScene().getWindow()).close();
    }
    @FXML
    private void handleCancle(){
        //connectDialog.getScene().getWindow().hide();
        ((Stage)connectDialog.getScene().getWindow()).close();
    }

    private HwDateSource dialogDBInfo(){
        return new HwDateSource(dbType,dbIpTextField.getText(),dbPortTextField.getText(),dbNameTextField.getText(),dbUserTextField.getText(),dbPasswordField.getText());
    }
}
